// pages/paper/paper.js
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    time:0,
    topic_list:[],
    show:false,
    complete:{}
  },

  /**交卷 */
  commit(e){
    let para = {
      paperId:this.data.id,
      reply:JSON.stringify(this.data.topic_list)
    }
    let that = this;
    api.curlPost('?cla=paperManagerApp&fun=commitAllTopic',para).then((res)=>{
      api.curlPost('?cla=paperManagerApp&fun=completePage',{id:that.data.id}).then((back)=>{
        that.setData({
          show:true,
          complete:back.data.result
        })
      })
    })
  },

/**复盘考试 */
seeRecord(e){
  wx.navigateTo({
    url: '../paperRecord/paperRecord?id='+e.currentTarget.dataset.id,
  })
},
/**继续考试 */
continueExam(e){
  wx.navigateTo({
    url: '../exam/exam',
  })
},

  /**单选框选择 */
  onRadioChange(event) {

    let topicId = event.currentTarget.dataset.id;
    let radio =  event.detail;
    let topic_list = this.data.topic_list;
    topic_list.forEach((v,i)=>{
      if(v.id==topicId){
        v.radio=radio
      }
    })
    this.setData({
      topic_list
    });

  },

  /**多选框 */
  onCheckBoxChange(event){
    let topicId = event.currentTarget.dataset.id;
    let checked =  event.detail;
    let topic_list = this.data.topic_list;
    topic_list.forEach((v,i)=>{
      if(v.id==topicId){
        v.radio=checked
      }
    })
    this.setData({
      topic_list
    });
  },
  setTextArea(e){
    let topicId = e.currentTarget.dataset.id;
    let topic_list = this.data.topic_list;
    console.log(e.detail.value,topicId)
    topic_list.forEach((v,i)=>{
      if(v.id==topicId){
        v.radio=e.detail.value
      }
    })
    console.log(topic_list)
    this.setData({
      topic_list
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if(!api.isBlank(options.id)){
      api.curlPost('?cla=paperManagerApp&fun=testPaper',{id:options.id}).then((res)=>{
          that.setData({
            id:res.data.result.id,
            topic_list:res.data.result.topicItem,
            time:res.data.result.endTime*1000
          })
      });
    }else{
      setTimeout(function(){
        wx.navigateBack({
          delta: 1,
        })
      },3000)
    }

  },
  finished(){
    this.commit()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})